﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShuruMimaUIController : MonoBehaviour {

    public Transform tf_AllUI;
    private int i_AllUIChildCount;
    private Color32 c_hitColor = new Color32(255, 241, 255, 255);       //高亮状态
    private Color32 c_notHitColor = new Color32(230, 67, 255, 255);
    public Transform tf_mingMima;     //明文密码父集
    public Transform tf_yinMima;      //隐藏密码父集（*）
    private int i_mimaIndex;          //密码的索引
    private int i_RightNum;           //判断密码正确的个数
    // Use this for initialization
    void Start () {
        i_AllUIChildCount = tf_AllUI.childCount;
        EventManager<bool>.AddEvent(MyEventType.UpButtonClickEvent, OnUpButtonClick);
        EventManager<bool>.AddEvent(MyEventType.DownButtonClickEvent, OnDownButtonClick);
        EventManager<bool>.AddEvent(MyEventType.LeftButtonClickEvent, OnLeftButtonClick);
        EventManager<bool>.AddEvent(MyEventType.RightButtonClickEvent, OnRightButtonClick);
        EventManager<bool>.AddEvent(MyEventType.SetButtonClickEvent, OnSetButtonClick);
        EventManager<bool>.AddEvent(MyEventType.QuitButtonClickEvent, OnQuitButtonClick);
        EventManager<bool>.AddEvent(MyEventType.ChongzhiShujuEvent,Reset);
    }

    private void OnQuitButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO") {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "dingzhiguhuaGO")
                {
                    
                    TimerMngr.Instance.Create(() =>
                    {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);

                    break;
                }
            }
        }
    }

    private void OnSetButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO")
        {
            i_RightNum = 0;//判断密码正确的个数
            for (int i = 0; i < tf_mingMima.childCount; i++) {
                if (tf_mingMima.GetChild(i).GetChild(0).GetComponent<Text>().text != "8") {
                    break;
                }
                i_RightNum++;
            }
            if (i_RightNum==4)
            {
                print("密码正确！");
                for (int i = 0; i < i_AllUIChildCount; i++)
                {
                    if (tf_AllUI.GetChild(i).name == "mainUI")
                    {
                        TimerMngr.Instance.Create(() =>
                        {
                            EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                            tf_AllUI.GetChild(i).SetAsLastSibling();
                        }).Start(0.1f);
                        break;
                    }
                }
                ShijianJiluUIController._instance.b_isSet = true;
            }
            else {
                print("密码有误！");
            }
        }
    }

    private void OnRightButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO")
        {
            if (i_mimaIndex < tf_mingMima.childCount - 1) {
                ShowMimaByIndex("right");
            }
        }
    }

    private void OnLeftButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO")
        {
            if (i_mimaIndex > 0) {
                ShowMimaByIndex("left");
            }
        }
    }

    private void OnDownButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO")
        {
            int temp = int.Parse(tf_mingMima.GetChild(i_mimaIndex).GetChild(0).GetComponent<Text>().text);//字符转整型
            temp--;
            if (temp <= 0) {
                temp = 0;
            }
            tf_mingMima.GetChild(i_mimaIndex).GetChild(0).GetComponent<Text>().text = temp.ToString();
        }
    }

    private void OnUpButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO")
        {
            int temp = int.Parse(tf_mingMima.GetChild(i_mimaIndex).GetChild(0).GetComponent<Text>().text);//字符转整型
            temp++;
            if (temp >= 9)
            {
                temp = 9;
            }
            tf_mingMima.GetChild(i_mimaIndex).GetChild(0).GetComponent<Text>().text = temp.ToString();
        }
    }

    /// <summary>
    /// 根据索引值显示隐藏密码
    /// </summary>
    /// <param name="dir"></param>
    private void ShowMimaByIndex(string dir) {
        UIController._instance.ChangeGaoliang(tf_mingMima, i_mimaIndex, false);
        tf_mingMima.GetChild(i_mimaIndex).GetChild(0).gameObject.SetActive(false);
        switch (dir)
        {
            case "right":i_mimaIndex++;break;
            case "left":i_mimaIndex--;break;
            default:break;
        }
        UIController._instance.ChangeGaoliang(tf_mingMima, i_mimaIndex, true);
        tf_mingMima.GetChild(i_mimaIndex).GetChild(0).gameObject.SetActive(true);
        //“*”密码的显示
        for (int i = 0; i < tf_yinMima.childCount; i++) {
            tf_yinMima.GetChild(i).gameObject.SetActive(true);
            if (i == i_mimaIndex) {
                tf_yinMima.GetChild(i).gameObject.SetActive(false);
            }
        }
    }

    /// <summary>
    /// 重置
    /// </summary>
    private void Reset(bool active) {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "shurumimaGO") {
            for (int i = 0; i < 3; i++) {
                if(i_mimaIndex >= 1 && i_mimaIndex <= 3)
                    ShowMimaByIndex("left");
            }
            i_RightNum = 0;
        }     
    }
}
